package web;

import entity.Msg;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;

/***
 * Author: Mr.Zhao, Time: 2019/5/22/20:18
 * Description:
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    @ResponseBody
    public Msg test() {
        return Msg.success();
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Msg login(@RequestParam(value = "username") String username,
                     @RequestParam(value = "password") String password) {
        User user;
        user = userService.selectByUsername(username);
        if (user == null)  //用户不存在
        {
            return Msg.fail().setMsg("该用户不存在！");
        }
        user = userService.login(username, password);
        if (user == null) {
            return Msg.fail().setMsg("密码错误！");
        }
        System.out.println(user);
        return Msg.success().add("user", user);
    }

}
